void InitUSART1(void);
unsigned char getchar1(void);
void putchar1(char c);
void USARTPrint1(char Msg[],int Len);
unsigned char scan_usart1(void);
void USART1DispInt(int temp);
void USART1write(char * str);
void USART1writeln(char * str); //Displays message on new line
//void USART1DispDouble(double temp);
void USART1EchoDemo(void);



void InitUSART0(void);
unsigned char getchar0(void);
void putchar0(char c);
void USARTPrint0(char Msg[], int Len);
unsigned char scan_usart0(void);
void USART0DispInt(int temp);
void USART0write(char * str);
void USART0writeln(char * str); //Displays message on new line
void USART0EchoDemo(void);



//Defines for USART
#define 	RXB8 		1
#define 	TXB8 		0
#define 	UPE 		2
#define 	OVR 		3
#define 	FE 			4
#define 	UDRE 		5
#define 	RXC 		7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define ERROR_CHAR (unsigned char)'?'

//void USART0EchoDemo(void)
//{
//	USART0writeln("Demoing USART0");
//	char temp;
//	while (1)
//	{
//		for (int i=0;i<16;i++)
//		{
//			temp=getchar0();
//			LCDdispCHAR(temp);
//		}
//		LCDLine2();
//		for (int i=0;i<16;i++)
//		{
//			temp=getchar0();
//			LCDdispCHAR(temp);
//		}
//		LCDClear();
//	}
//
//
//
//}
//void USART1EchoDemo(void)
//{
//
//	char temp;
//	while (1)
//	{
//		for (int i=0;i<16;i++)
//		{
//			temp=getchar1();
//			LCDdispCHAR(temp);
//		}
//		LCDLine2();
//		for (int i=0;i<16;i++)
//		{
//			temp=getchar1();
//			LCDdispCHAR(temp);
//		}
//		LCDClear();
//	}
//	
//}
//
// appends string to usart view
void USART1write(char * str)
{
	USARTPrint1("$",1);
	USARTPrint1(str, strlen(str));
	USARTPrint1("$",1);
}



// prints new string to LCD view
void USART1writeln(char * str)
{
	USARTPrint1("$",1);
	USART1write("\n\r");
	USARTPrint1(str, strlen(str));
	USARTPrint1("$",1);
}

/*void USART1DispDouble(double temp)
{
	//displays a 6 digit double xxxx.xx

	int thousands, hundreds, tens, ones, tenths, hundreths;
	
	while (temp>9999)
		temp-=10000;

	thousands=(((temp/10)/10)/10)+48;
	hundreds=(((temp/10)/10)%10)+48;
	tens=((temp/10)%10)+48;
	ones=(temp%10)+48;
	
	while (temp>1)
		temp-=1;
	
	if (temp>=.9)
		tenths=9;
	else if (temp>=.8)
		tenths=.8;
	if (temp>=.7)
		tenths=7;
	else if (temp>=.6)
		tenths=.6;
	if (temp>=.5)
		tenths=5;
	else if (temp>=.4)
		tenths=.4;
	if (temp>=.3)
		tenths=3;
	else if (temp>=.2)
		tenths=.2;
	if (temp>=.1)
		tenths=1;
	else
		tenths =0;
		
	while(temp>.1)
		temp-=.1)
	
	if (temp>=.09)
		hundreths=9;
	if (temp>=.08)
		hundreths=8;
	if (temp>=.07)
		hundreths=7;
	if (temp>=.06)
		hundreths=6;
	if (temp>=.05)
		hundreths=5;
	if (temp>=.04)
		hundreths=4;
	if (temp>=.03)
		hundreths=3;
	if (temp>=.02)
		hundreths=2;
	if (temp>=.01)
		hundreths=1;
	else
		hundreths=0;
	
	

}

*/
void USART1DispInt(int temp)
{
	//Displays a 4 digit integer on the LCD
	
	int thousands, hundreds, tens, ones;
	
	while (temp>9999)
		temp-=10000;
	
	thousands=(((temp/10)/10)/10)+48;
	hundreds=(((temp/10)/10)%10)+48;
	tens=((temp/10)%10)+48;
	ones=(temp%10)+48;
	
	
	USARTPrint1("$",1);
	putchar1(thousands);
	putchar1(hundreds);
	putchar1(tens);
	putchar1(ones);
	USARTPrint1("$",1);
}


// appends string to usart view
void USART0write(char * str)
{
	USARTPrint0(str, strlen(str));
}


// prints new string to LCD view
void USART0writeln(char * str)
{
	USART0write("\n\r");
	USARTPrint0(str, strlen(str));

}




void USART0DispInt(int temp)
{
	//Displays a 4 digit integer on the LCD
	
	int thousands, hundreds, tens, ones;
	
	while (temp>9999)
		temp-=10000;
	
	thousands=(((temp/10)/10)/10)+48;
	hundreds=(((temp/10)/10)%10)+48;
	tens=((temp/10)%10)+48;
	ones=(temp%10)+48;
	
	
	putchar0(thousands);
	putchar0(hundreds);
	putchar0(tens);
	putchar0(ones);
}





void InitUSART1(void)
{

	// USART1 initialization
	// Communication Parameters: 8 Data, 1 Stop, No Parity
	// USART1 Receiver: On
	// USART1 Transmitter: On
	// USART1 Mode: Asynchronous
	// USART1 Baud rate: 9600
	UCSR1A=0x00;
	UCSR1B=0x18;     		//(00011000)
	UCSR1C=0x06;
	
	
	//9600 bps
	//UBRR1H=0x00;			//baud rate register high
	//UBRR1L=0x67;			//103   baud rate register low


	//57600 bps
	UBRR1H=0x00;			//baud rate register high
	UBRR1L=0x10;			//103   baud rate register low
	

}

void InitUSART0(void)
{

	// USART0 initialization 
   // Communication Parameters: 8 Data, 1 Stop, No Parity 
   // USART0 Receiver: On 
   // USART0 Transmitter: On 
   // USART0 Mode: Asynchronous 
   // USART0 Baud rate: 9600 
   UCSR0A=0x00; 
   UCSR0B=0x18;           //(00011000) 
   UCSR0C=0x06; 
   UBRR0H=0x00;         //baud rate register high 
   UBRR0L=0x67;         //103   baud rate register low 

}

unsigned char scan_usart1(void)
{
/*
	char status,data;
		if ((status=UCSR1A) & (1<<RXC)!=0)
			data=UDR1;
		if ((status & (1<<FE | 1<<UPE | 1<<OVR))==0) return data;
	
*/	
	
	//char status,data;
	//if (((status=UCSR1A) & RX_COMPLETE)!=0)
	//	  data=UDR1;         
	//if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)return data;
	
	if((UCSR1A & RX_COMPLETE) && !(UCSR1A & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))){
		return (unsigned char)UDR1;
	}
	else{
		return ERROR_CHAR;
	}
}

unsigned char scan_usart0(void) 
{ 
   //char status,data='!';
    //  if ((status=UCSR0A) & ((1<<RXC)!=0)) 
      //   data=UDR0;
      //if ((status & (1<<FE | 1<<UPE | 1<<OVR))==0)
		//return data;
	if((UCSR0A & RX_COMPLETE) && !(UCSR0A & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))){
		return (unsigned char)UDR0;
	}
	else{
		return ERROR_CHAR;
	}

	
} 

unsigned char getchar1(void)
{
	char status,data='!';
	while (1)
		  {
		  while (((status=UCSR1A) & RX_COMPLETE)==0);
		  data=UDR1;        
		  if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)   return data;               
									
		  };
} 


unsigned char getchar0(void)
{
	char status,data='!';
	while (1)
		  {
		  while (((status=UCSR0A) & RX_COMPLETE)==0);
		  data=UDR0;         
		  if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)   return data;               
									
		  };
} 

void putchar1(char c)
{
	while ((UCSR1A & 1<<UDRE)==0);
	UDR1=c;
} 


void putchar0(char c) 
{ 
   while ((UCSR0A & 1<<UDRE)==0); 
   UDR0=c; 
} 


/*
	Function: USARTPrint1
	
	Input:  Msg[]; string of characters to be printed
	
	Notes:  String must be terminated by '~'
	
	Description:  	This function will take in an unlimited
					length string of characters and print
					it to the USART.  It will terminate
					printing when it reads the character '~'.
*/
void USARTPrint1(char Msg[], int Len)
{
	int i=0;
	char temp;
	
	temp=Msg[i];
	while (i!=Len)
	{
		putchar1(temp);
		i++;
		temp=Msg[i];
	}
	


}

/*
	Function: USARTPrint0
	
	Input:  Msg[]; string of characters to be printed
	
	Notes:  String must be terminated by '~'
	
	Description:  	This function will take in an unlimited
					length string of characters and print
					it to the USART.  It will terminate
					printing when it reads the character '~'.
*/

void USARTPrint0(char Msg[], int Len)
{
	int i=0;
	char temp;
	
	temp=Msg[i];
	while (i!=Len)
	{
		putchar0(temp);
		i++;
		temp=Msg[i];
	}
	


}
